package com.jml.zlhq.app.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.util.UIUtil;

import android.content.Intent;
import android.graphics.Matrix;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author lenovo 分享赚
 */
public class FenxianZuanFragment extends BaseFragment {
	private ViewPager viewPager;// 页卡内容
	// private ImageView imageView;// 动画图片
	private TextView tupian, duanzi, zishi;
	private List<Fragment> views;// Tab页面列表

	private int currIndex;
	private ImageView cursor;
	private int offset = 0;// 动画图片偏移量
	private int bmpW;// 动画图片宽度

	// 用于展示推荐的Fragment
	private FenxiangZuanTypeFragment tupianf, duanzif, zishif;

	// private PullToRefreshListView mPullToRefreshView;
	// private ListView listView;
	// private IBaseAdapter adapter;
	// private List<JSONObject> listItems = new ArrayList<JSONObject>();
	// public static boolean freshPage;
	// private Map<String,String> conditionMap;
	//
	// @SuppressLint("HandlerLeak")
	// private Handler mHandler = new Handler(){
	// @Override
	// public void handleMessage(Message msg) {
	// switch (msg.what) {
	// case 1:
	// adapter.notifyDataSetChanged();
	// mPullToRefreshView.onRefreshComplete();
	// if(pageEnd)mPullToRefreshView.setMode(Mode.PULL_FROM_START);
	// break;
	// default:
	// break;
	// }
	// }
	// };

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		LinearLayout leftLay = (LinearLayout) pageView
				.findViewById(R.id.content_header_left_lay);
		LinearLayout rightLay = (LinearLayout) pageView
				.findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		rightLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) pageView
				.findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) pageView
				.findViewById(R.id.content_header_left_img);
		leftImage.setVisibility(View.INVISIBLE);
		ImageView roghtImage1 = (ImageView) pageView
				.findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.INVISIBLE);
		TextView centerText = (TextView) pageView
				.findViewById(R.id.content_header_center_text);
		centerText.setText("分享赚");
		TextView rightText = (TextView) pageView
				.findViewById(R.id.content_header_right_text);
		rightText.setVisibility(View.INVISIBLE);
		TextView centerChosed = (TextView) pageView
				.findViewById(R.id.content_header_center_text2);
		centerChosed.setVisibility(View.GONE);

		cursor = (ImageView) pageView.findViewById(R.id.main_faxian_cursor);

		viewPager = (ViewPager) pageView
				.findViewById(R.id.mine_fenxianzhuan_viewpager);
		viewPager.setOffscreenPageLimit(1);
		views = new ArrayList<Fragment>();
		tupianf = new FenxiangZuanTypeFragment(1);
		duanzif= new FenxiangZuanTypeFragment(2);
		zishif  = new FenxiangZuanTypeFragment(3);
		views.add(tupianf);
		views.add(duanzif);
		views.add(zishif);
		
		viewPager.setOffscreenPageLimit(0);
		viewPager
				.setAdapter(new MyViewPagerAdapter(getFragmentManager(), views));
		viewPager.setCurrentItem(0);
		viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
		tupian = (TextView) pageView
				.findViewById(R.id.mine_faxian_tupian);
		duanzi = (TextView) pageView
				.findViewById(R.id.mine_faxian_duanzi);
		zishi = (TextView) pageView.findViewById(R.id.mine_faxian_zishi);

		tupian.setOnClickListener(new MyOnClickListener(0));
		duanzi.setOnClickListener(new MyOnClickListener(1));
		zishi.setOnClickListener(new MyOnClickListener(2));

		GradientDrawable gd = new GradientDrawable();// 创建drawable
		gd.setColor(getResources().getColor(R.color.color_e95847));
		gd.setSize(UIUtil.winWidth / 3, UIUtil.dip2px(appContext, 2));
		cursor.setImageDrawable(gd);
		bmpW = UIUtil.winWidth / 3;// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		offset = (screenW / 3 - bmpW) / 2;// 计算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置

		tupian.setTextColor(getResources().getColor(R.color.color_e95847));

		// mPullToRefreshView = (PullToRefreshListView)
		// pageView.findViewById(R.id.main_faxian_listview);
		// mPullToRefreshView.setOnRefreshListener(new
		// OnRefreshListener<ListView>() {
		//
		// @Override
		// public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		// if(PullToRefreshBase.Mode.PULL_FROM_START ==
		// mPullToRefreshView.getCurrentMode()){
		// String label =
		// DateUtils.formatDateTime(appContext,System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME
		// | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
		// mPullToRefreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
		// clearData();
		// loadData(null);
		// }else if(PullToRefreshBase.Mode.PULL_FROM_END ==
		// mPullToRefreshView.getCurrentMode()){
		// if(urlParam==null)urlParam = new HashMap<String,String>();
		// pageIndex++;
		// urlParam.put("pageNo", String.valueOf(pageIndex));
		// loadData(urlParam);
		// }
		// }
		// });
		// listView = mPullToRefreshView.getRefreshableView();
		// //listView.setDividerHeight(UIUtil.dip2px(appContext, 10));
		// adapter = new MainFaXianAdapter(activity, listItems,true);
		// listView.setAdapter(adapter);
		// listView.setOnItemClickListener(new OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> parent, View view, int
		// position, long id) {
		// int index = new Long(id).intValue();
		// Bundle data = new Bundle();
		// data.putString("title", "活动详情");
		// data.putString("url", BaseTools.getFrom("url",
		// listItems.get(index)));
		// UIUtil.openActivity(activity, CommonWebActivity.class, data);
		// }
		// });
		//
		// freshPage = false;
		// loadData(null);

		return pageView;
	}

	@Override
	public void onResume() {
		// pageFresh = freshPage;
		super.onResume();
		// freshPage = false;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// if(requestCode==4&&resultCode==Activity.RESULT_OK&&null!=data){
		// conditionMap = (Map<String,String>)
		// data.getSerializableExtra("data");
		// if(urlParam==null)urlParam = new HashMap<String,String>();
		// urlParam.clear();
		// urlParam.put("pageNo", String.valueOf(1));
		// urlParam.putAll(conditionMap);
		// listItems.clear();
		// loadData(urlParam);
		// }
	}

	@Override
	public void loadData(Map<String, String> param) {
		// if(param==null){
		// param = new HashMap<String, String>();
		// param.put("pageNo", String.valueOf(pageIndex));
		// }
		//
		// HttpService.getByText(URLs.MAIN_FAXIAN_SEARCH, param, new
		// TextHttpResponseHandler() {
		// @Override
		// public void onSuccess(int arg0, Header[] arg1, String resultStr) {
		// try {
		// if(BuildConfig.DEBUG)Log.i("==result post==", resultStr);
		// JSONObject result = new JSONObject(resultStr);
		// if(result.getInt("status")==0){
		// JSONArray data = result.get("data").toString().equals("null")?new
		// JSONArray():result.getJSONArray("data");
		// for (int i = 0; data!=null && i < data.length(); i++) {
		// listItems.add(data.getJSONObject(i));
		// }
		// }else{
		// if(pageIndex>0)pageIndex--;
		// }
		// mHandler.sendEmptyMessage(1);
		//
		// } catch (JSONException e) {
		// e.printStackTrace();
		// }
		// }
		//
		// @Override
		// public void onFailure(int arg0, Header[] arg1, String arg2, Throwable
		// arg3) {
		// UIUtil.ToastMessage(activity, "抱歉，访问失败");
		// }
		// });
		// listItems.add(new JSONObject());
		// adapter.notifyDataSetChanged();
		// mPullToRefreshView.onRefreshComplete();
	}

	@Override
	public void clearData() {
		// pageEnd = false;
		// if(urlParam==null)urlParam = new HashMap<String,String>();
		// pageIndex=1;
		// urlParam.put("pageNo", String.valueOf(pageIndex));
		// adapter.setPageEnd(pageEnd);
		// mPullToRefreshView.setMode(Mode.BOTH);
		// listItems.clear();
	}

	@Override
	public void onClick(View v) {
		// switch (v.getId()) {
		// case R.id.content_header_left_lay:
		// UIUtil.openActivity(activity, SearchActivity.class, null);
		// break;
		// case R.id.content_header_right_lay:
		// Bundle data = new Bundle();
		// data.putString("url", URLs.MAIN_FAXIAN_CONDITION);
		// data.putSerializable("condition", (Serializable) conditionMap);
		// UIUtil.openActivity(this, ConditionActivity.class, data, 4);
		// break;
		// default:
		// break;
		// }
	}

	@Override
	protected int getPageView() {
		return R.layout.main_fenxiangzuan;
	}

	/**
	 * 
	 * 头标点击监听 3
	 */
	private class MyOnClickListener implements OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		public void onClick(View v) {
			viewPager.setCurrentItem(index);
		}

	}

	public class MyViewPagerAdapter extends FragmentPagerAdapter {
		private List<Fragment> mListViews;

		public MyViewPagerAdapter(FragmentManager mFragmentManager,
				List<Fragment> mListViews) {
			super(mFragmentManager);
			this.mListViews = mListViews;
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Fragment getItem(int arg0) {
			return mListViews.get(arg0);
		}
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		public void onPageSelected(int arg0) {
			int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
			tupian.setTextColor(getResources().getColor(R.color.color_333));
			tupian.setTextColor(getResources().getColor(R.color.color_333));
			zishi.setTextColor(getResources().getColor(R.color.color_333));
			switch (arg0) {
			case 0:
				tupian.setTextColor(getResources().getColor(
						R.color.color_e95847));
				break;
			case 1:
				tupian.setTextColor(getResources().getColor(
						R.color.color_e95847));
				break;
			case 2:
				zishi.setTextColor(getResources().getColor(
						R.color.color_e95847));
				break;
			}

			Animation animation = new TranslateAnimation(one * currIndex, one
					* arg0, 0, 0);
			currIndex = arg0;
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(300);
			cursor.startAnimation(animation);
		}

	}

}
